//
//  InputUserInfoViewModel.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2018/11/6.
//  Copyright © 2018年 HCreater. All rights reserved.
//

#import "InputUserInfoViewModel.h"

@interface InputUserInfoViewModel()

@property (nonatomic, strong) RACCommand* reqCmd;

@end

@implementation InputUserInfoViewModel

#pragma mark - Ovrride
- (void)initialize
{
    [super initialize];
    
    @weakify(self)
    
    _type = 0; //0:TF 1:TextView 2:Company2 3:Company3
    
    _editable = YES;
    
    self.reqCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input)
    {
        @strongify(self);
     
        return [self _requestSignal];
    }];
}

- (RACSignal*)_requestSignal
{
    @weakify(self);
    
    UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
    
    NSString* url = @"";
    NSDictionary* postData = nil;
    
    if ([self.reqType isEqualToString:@"USER_NICKNAME"])
    {
        url = url_interface_user_perfectuploads;
        postData = interface_param_user_perfectuploads(um.userData.userID, self.strTF, @"", @"", @"", @"");
    }
    else if ([self.reqType isEqualToString:@"EXPERT_INFO"])
    {
        url = url_interface_summaryedit;
        postData = interface_param_summaryedit(um.expertData.expertInfo.expertID, @"", @"", self.strCompanyIntro, @"0");
    }
    else if ([self.reqType isEqualToString:@"MECHANISM_INTRO"])
    {
        url = url_interface_institution_summaryedit;
        postData = interface_param_institution_summaryedit(um.institutionData.institutionInfo.institutionID, @"", @"", @"", @"", @"", @"", @"", self.strText, @"0");
    }
    else if ([self.reqType isEqualToString:@"MECHANISM_INFO"])
    {
        url = url_interface_institution_summaryedit;
        postData = interface_param_institution_summaryedit(um.institutionData.institutionInfo.institutionID, @"", self.strCompanyMobile, self.strCompanyContacter, @"", self.strCompanyAddr, @"", @"", @"", @"0");
    }
    else if ([self.reqType isEqualToString:@"MECHANISM_SERV"])
    {
        url = url_interface_institution_summaryedit;
        postData = interface_param_institution_summaryedit(um.institutionData.institutionInfo.institutionID, @"", @"", @"", @"", @"", @"", self.strText, @"", @"0");
    }
    else if ([self.reqType isEqualToString:@"COMPANY_INTRO"])
    {
        url = url_interface_enterprise_summaryedit;
        postData = interface_param_enterprise_summaryedit(um.enterpriseData.enterpriseInfo.enterpriseID, @"", @"", @"", @"", @"", self.strText, @"0");
    }
    else if ([self.reqType isEqualToString:@"COMPANY_INFO"])
    {
        url = url_interface_enterprise_summaryedit;
        postData = interface_param_enterprise_summaryedit(um.enterpriseData.enterpriseInfo.enterpriseID, self.strCompanyMobile, self.strCompanyContacter, @"", @"", self.strCompanyAddr, @"", @"0");
    }
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber)
    {
        @strongify(self);
        
        [[AFNetWorkUtils racPOSTWthURL:url params:postData] subscribeNext:^(id x)
        {
            [subscriber sendNext:nil];
            [subscriber sendCompleted];
        } error:^(NSError *error)
        {
            NSString* strError = [error.userInfo[customErrorInfoKey] isNotBlank] ? error.userInfo[customErrorInfoKey] : @"提交失败";
            [subscriber sendError:[NSError errorWithDomain:SUCommandErrorDomain code:SUCommandErrorCode userInfo:@{SUCommandErrorUserInfoKey:strError}]];
            [subscriber sendCompleted];
        }];
        
        return nil;
    }];
}

@end
